<template>
  <li class="ProgramItem" @click="play(item.mainSong.id)">
    <div class="order">
      <span>{{ index }}</span>
    </div>
    <div class="program_info">
      <p class="program_name">{{ item.name }}</p>
      <p class="program_data">
        <span>{{item.createTime | FormatTime}}</span>
        <span><i class="icon"></i>{{item.listenerCount | FormatCount}}</span>
        <span><i class="icon"></i>{{item.duration | FormatDuration}}</span>
      </p>
      <template>
        <div class="play_icon_current" v-if="$store.state.play.currentMusic?$store.state.play.currentMusic.id == item.mainSong.id:false">
          <i :class="{pause:!$store.state.play.icon_state}"></i>
          <i :class="{pause:!$store.state.play.icon_state}" ></i>
          <i :class="{pause:!$store.state.play.icon_state}" ></i>
          <i :class="{pause:!$store.state.play.icon_state}" ></i>
        </div>
        <span class="play_icon" v-else></span>
      </template>
    </div>
  </li>
</template>

<script>
export default {
  props: ["item", "index"],
  filters:{
    FormatTime(InitValue){
      var TimeStr = new Date(InitValue);
      var Y = TimeStr.getFullYear();
      var m = (TimeStr.getMonth()+1).toString().padStart(2,0);
      var d = TimeStr.getDate().toString().padStart(2,0);
      return Y + '-' + m + '-' + d
    },
    FormatCount(InitValue){
      if(InitValue < 10**4){
        return InitValue
      }else{
        return (InitValue / 10**4).toString().split(".")[0] + '万'
      }
    },
    FormatDuration(InitValue){
      var m = (InitValue / 1000 / 60).toString().split(".")[0];
      var s = (InitValue / 1000 % 60).toString().split(".")[0];
      if( m >= 1){
        return m.padStart(2,0) + ":" + s.padStart(2,0);
      }else{
        return '00:' + s.padStart(2,0);
      }
    }
  },
  data(){
    return {
      lyricStr:null,
    }
  },
  methods:{
    play(id){
      this.$store.state.play.currentMusic = {
        id,
        al:{
          name:this.item.name,
          picUrl:this.item.dj.avatarUrl,
        },
        dj:this.item.dj,
        name:this.item.name,
        mainSong:this.item.mainSong
      }
      this.axios.get("/lyric?id=" + id).then(response=>{
        if(!response.data.lrc){
          this.$store.state.play.lyric = [{text:'暂无歌词'}]
          return;
        }
        this.lyricStr = response.data.lrc.lyric;
        var patt = /\[\d{2}:\d{2}\.\d{2,3}\]/gi;
        this.lyricStr = this.lyricStr
        .split("\n")
        .filter(e=>e)
        .map(str=>{
            var time = str.match(patt)[0].replace(/(\[|\])/gi,"");
            var timeArr = time.split(":");
            time = Number(timeArr[0]) * 60 + Number(timeArr[1]);
            var text = str.replace(patt,"");
            return {time,text};
        })
        this.$store.state.play.lyric = this.lyricStr
      });
    }
  }
};
</script>

<style lang="less">
.ProgramItem {
  width: 100%;
  height: 50px;
  display: flex;
  .order {
    width: 60px;
    height: 50px;
    text-align: center;
    line-height: 50px;
    color: #999;
    font-size: 18px;
  }
  .program_info {
    width: calc(100% - 60px);
    height: 50px;
    border-bottom: 1px solid #f6f6f6;
    padding-top: 10px;
    position: relative;
    .program_name {
      width: 80%;
      overflow: hidden;
      white-space: nowrap;
      word-break: break-all;
      text-overflow: ellipsis;
    }
    .program_data{
      font-size: 12px;
      color: #999;
      margin-top: 5px;
      span{
        margin-right: 10px;
        &:nth-child(2){
          i.icon{
            display: inline-block;
            width: 16px;
            height: 16px;
            vertical-align: -4px;
            background-image: url();
            background-repeat: no-repeat;
            background-size: 16px;
          }
        }
        &:nth-child(3){
          i.icon{
            display: inline-block;
            width: 16px;
            height: 16px;
            vertical-align: -5px;
            background-image: url();
            background-repeat: no-repeat;
            background-size: 12px;
          }
        }
      }
    }
    .play_icon_current{
      position: absolute;
      right: 10px;
      top: 14px;
      i{
        display: inline-block;
        width: 3px;
        height: 22px;
        background-color: red;
        margin-left: 3px;
        animation: jump .6s infinite -0.4s linear alternate;
        transform-origin: center bottom;
        &:nth-child(2){
          animation-delay: -0.2s;
        }
        &:nth-child(3){
          animation-delay: 0s;
        }
        &:nth-child(4){
          animation-delay: 0.2s;
        }
        &.pause{
          animation-play-state: paused;
        }
      }
      @keyframes jump {
        0%{
        }
        100%{
          transform: scaleY(0);
        }
      }
    }
    .play_icon {
      position: absolute;
      right: 10px;
      top: 14px;
      display: inline-block;
      width: 22px;
      height: 22px;
      background: url(https://s3.music.126.net/mobile-new/img/index_icon_2x.png?5207a28…=)
        no-repeat;
      background-position: -24px 0;
      background-size: 166px 97px;
    }
  }
}
</style>